{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "!pip install --upgrade pip\n",
    "!yes | pip uninstall dialogflow\n",
    "!pip install dialogflow==0.3.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hit **Reset Session** > **Restart**, then resume with the following cells. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from google.cloud import datastore\n",
    "\n",
    "client = datastore.Client()\n",
    "query = client.query(kind='Topic')\n",
    "results = list(query.fetch())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import dialogflow\n",
    "\n",
    "entity_types_client = dialogflow.EntityTypesClient()\n",
    "\n",
    "project_id = !(gcloud config get-value project)\n",
    "\n",
    "project_agent_path = entity_types_client.project_agent_path(\n",
    "        project_id[0])\n",
    "\n",
    "for element in entity_types_client.list_entity_types(project_agent_path):\n",
    "  if (element.display_name == 'Topic'):\n",
    "    entity_type_path = element.name\n",
    "\n",
    "project_id = !(gcloud config get-value project)\n",
    "\n",
    "entities = []\n",
    "\n",
    "for result in results:\n",
    "  \n",
    "  entity = dialogflow.types.EntityType.Entity()\n",
    "  entity.value = result.key.name\n",
    "  entity.synonyms.append(result.key.name)\n",
    "\n",
    "  entities.append(entity)\n",
    "\n",
    "print entities\n",
    "\n",
    "response = entity_types_client.batch_create_entities(\n",
    "        entity_type_path, entities)\n",
    "\n",
    "print('Entity created: {}'.format(response))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
